package com.xueyi.game.backend.controller;

import com.xueyi.core.base.BaseBackController;
import com.xueyi.core.base.BaseResponse;
import com.xueyi.core.base.annotation.Add;
import com.xueyi.core.base.annotation.Update;
import com.xueyi.core.expection.BusinessException;
import com.xueyi.game.backend.entity.dto.AddOrUpdateJurisdictionDto;
import com.xueyi.game.backend.entity.dto.DeleteJurisdictionByIdDto;
import com.xueyi.game.backend.entity.dto.FindAllJurisdictionDto;
import com.xueyi.game.backend.entity.dto.RemoveMenuByIdDto;
import com.xueyi.game.backend.entity.vo.BaseMenuVo;
import com.xueyi.game.backend.entity.vo.BaseSelectVo;
import com.xueyi.game.backend.entity.vo.FindAllFatherMenuVo;
import com.xueyi.game.backend.service.BaseJurisdictionService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: hxs
 * @createDate: 2022/11/9 16:09
 * @description: 权限管理接口
 */
@RestController
@AllArgsConstructor
public class BaseJurisdictionController extends BaseBackController {
    private final BaseJurisdictionService baseJurisdictionService;

    /**
     * 获取所有的权限
     *
     * @param findAllJurisdictionReq
     * @return
     * @author: hxs
     */
    @GetMapping("/jurisdiction/findAllJurisdiction")
    public BaseResponse<List<BaseMenuVo>> findAllJurisdiction(@Validated FindAllJurisdictionDto findAllJurisdictionReq) {
        List<BaseMenuVo> response = baseJurisdictionService.findAllJurisdiction(findAllJurisdictionReq);
        return BaseResponse.success(response);
    }

    /**
     * 新增权限
     *
     * @param addOrUpdateJurisdictionReq
     * @return
     * @author: hxs
     */
    @PostMapping("/jurisdiction/addJurisdiction")
    public BaseResponse<Boolean> addJurisdiction(@Validated(Add.class) @RequestBody AddOrUpdateJurisdictionDto addOrUpdateJurisdictionReq) throws BusinessException {
        baseJurisdictionService.addJurisdiction(addOrUpdateJurisdictionReq);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 修改权限
     *
     * @param addOrUpdateJurisdictionReq
     * @return
     * @author hxs
     */
    @PostMapping("/jurisdiction/updateJurisdiction")
    public BaseResponse<Boolean> updateJurisdiction(@Validated(Update.class) @RequestBody AddOrUpdateJurisdictionDto addOrUpdateJurisdictionReq) throws BusinessException {
        baseJurisdictionService.updateJurisdiction(addOrUpdateJurisdictionReq);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 获取所有的顶级菜单
     *
     * @return
     * @author hxs
     */
    @GetMapping("/jurisdiction/findAllFatherMenu")
    public BaseResponse<FindAllFatherMenuVo> findAllFatherMenu() {
        List<BaseSelectVo> menu = baseJurisdictionService.findAllFatherMenu(1);
        List<BaseSelectVo> jurisdiction = baseJurisdictionService.findAllFatherMenu(2);
        FindAllFatherMenuVo response = new FindAllFatherMenuVo(menu, jurisdiction);
        return BaseResponse.success(response);
    }

    /**
     * 删除权限
     *
     * @param deleteJurisdictionByIdReq
     * @return
     * @author hxs
     */
    @PostMapping("/jurisdiction/deleteJurisdictionById")
    public BaseResponse<Boolean> deleteJurisdictionById(@Validated @RequestBody DeleteJurisdictionByIdDto deleteJurisdictionByIdReq) {
        baseJurisdictionService.deleteJurisdictionById(deleteJurisdictionByIdReq);
        return BaseResponse.success(Boolean.TRUE);
    }

    /**
     * 物理删除权限
     *
     * @param removeMenuByIdReq
     * @return
     * @author hxs
     */
    @PostMapping("/jurisdiction/removeMenuById")
    public BaseResponse<Boolean> removeMenuById(@Validated @RequestBody RemoveMenuByIdDto removeMenuByIdReq) {
        baseJurisdictionService.removeMenuById(removeMenuByIdReq);
        return BaseResponse.success(Boolean.TRUE);
    }
}
